package CollectionDemo;

import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
/*
private class Itr implements Iterator<E> {
        int cursor;       // index of next element to return
        int lastRet = -1; // index of last element returned; -1 if no such
        int expectedModCount = modCount;

        // prevent creating a synthetic constructor
        Itr() {}

        public boolean hasNext() {
            return cursor != size;
        }

        @SuppressWarnings("unchecked")
        public E next() {
            checkForComodification();
            int i = cursor;
            if (i >= size)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i + 1;
            return (E) elementData[lastRet = i];
        }

        public void remove() {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
                ArrayList.this.remove(lastRet);
                cursor = lastRet;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }

        @Override
        public void forEachRemaining(Consumer<? super E> action) {
            Objects.requireNonNull(action);
            final int size = ArrayList.this.size;
            int i = cursor;
            if (i < size) {
                final Object[] es = elementData;
                if (i >= es.length)
                    throw new ConcurrentModificationException();
                for (; i < size && modCount == expectedModCount; i++)
                    action.accept(elementAt(es, i));
                // update once at end to reduce heap write traffic
                cursor = i;
                lastRet = i - 1;
                checkForComodification();
            }
        }

        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }
 */

//看一看ArrayList的迭代器是如何实现的
//首先当然
public class ListIterator implements Iterator
{

    private int cursor; //下一个元素的索引

    //index of last element returned; -1 if no such,上一次next的元素索引
    private int lastRet = -1;
    private ArrayList arrayList;

    public ListIterator(ArrayList arrayList) {
        this.arrayList = arrayList;
    }

    //如果被迭代的集合还没有被遍历，则返回true
    public boolean hasNext() {
        return cursor != arrayList.size();
    }

    public Object next() {
        int i = cursor; //获取next的索引
        if (i >= arrayList.size()) //判断该索引的值是否大于list的size
            throw new NoSuchElementException();
        Object[] elementData = arrayList.toArray(); //将list转化为数组
        if (i >= elementData.length)
            throw new ConcurrentModificationException();
        cursor = i + 1; //将指针指向右移一位
        return elementData[lastRet = i]; //lastRet指向此次遍历的位置，并返回该元素
    }

    public void remove() {
        if (lastRet < 0)
            throw new IllegalStateException();


        try {
            arrayList.remove(lastRet); //删除上次next的元素
            cursor = lastRet; //将cursor指针索引左移一位
            lastRet = -1; //？

        } catch (IndexOutOfBoundsException ex) {
            throw new ConcurrentModificationException();
        }
    }

    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        arrayList.add("十七");
        arrayList.add("你好");
        arrayList.add("吵架");
        ListIterator listIterator = new ListIterator(arrayList);
        while (listIterator.hasNext()) {
            System.out.println(listIterator.next());
        }

    }

}
